/***************************************************************************
                         qgsquantizedmeshdataprovider.h
                         --------------------
    begin                : June 2024
    copyright            : (C) 2024 by David Koňařík
    email                : dvdkon at konarici dot cz
 ******************************************************************
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#pragma once

#include "qgis.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsprovidermetadata.h"
#include "qgstiledsceneboundingvolume.h"
#include "qgstiledscenedataprovider.h"
#include "qgstiledsceneindex.h"
#include "qgstiles.h"
#include <optional>
#include <qmap.h>
#include <qvector.h>

#define SIP_NO_FILE

///@cond PRIVATE

class CORE_EXPORT QgsQuantizedMeshDataProvider: public QgsTiledSceneDataProvider
{
    Q_OBJECT
  public:
    struct Metadata
    {
      QgsRectangle mExtent;
      QgsTiledSceneBoundingVolume mBoundingVolume;
      // Map of zoom level -> list of AABBs of available tiles (tile index ranges)
      QVector<QVector<QgsTileRange>> mAvailableTiles;
      QgsCoordinateReferenceSystem mCrs;
      QString mTileScheme;
      uint8_t mMinZoom;
      uint8_t mMaxZoom;
      std::vector<QString> mTileUrls;
    };

    QgsQuantizedMeshDataProvider( const QString &uri,
                                  const QgsDataProvider::ProviderOptions &providerOptions,
                                  Qgis::DataProviderReadFlags flags = Qgis::DataProviderReadFlags() );
    Qgis::TiledSceneProviderCapabilities capabilities() const override;
    QgsTiledSceneDataProvider *clone() const override;
    const QgsCoordinateReferenceSystem sceneCrs() const override;
    const QgsTiledSceneBoundingVolume &boundingVolume() const override;
    QgsTiledSceneIndex index() const override;
    QgsDoubleRange zRange() const override;
    QgsCoordinateReferenceSystem crs() const override;
    QgsRectangle extent() const override;
    bool isValid() const override;
    QString name() const override;
    QString description() const override;

  private:
    QString mUri; // For clone()
    QgsDataProvider::ProviderOptions mProviderOptions; // For clone()
    bool mIsValid = false;
    Metadata mMetadata;
    std::optional<QgsTiledSceneIndex> mIndex;
};

class QgsQuantizedMeshProviderMetadata : public QgsProviderMetadata
{
    Q_OBJECT

  public:
    QgsQuantizedMeshProviderMetadata();
    QgsDataProvider *createProvider( const QString &uri,
                                     const QgsDataProvider::ProviderOptions &providerOptions,
                                     Qgis::DataProviderReadFlags flags = Qgis::DataProviderReadFlags() );
};

///@endcond
